#include "Log.hpp"
#include"TcpServer.hpp"
#include"protocol.hpp"
#include <cstdlib>
#include <string>
#include <memory.h>
#include <memory>

using namespace ns_protocol;
using namespace ns_tcpserver;

//计算方法
static Response calculatorHelper2(const Request& req)
{
    Response resp(req._x,req._y,req._op,0,0);
    switch (req._op) 
    {
    case '+':
        resp._result=req._x+req._y;
        break;
    case '-':
        resp._result=req._x-req._y;
        break;
    case '*':
        resp._result=req._x*req._y;
        break;
    case '/':
        if(req._y == 0){
            resp._code=1;
        }
        else {
            resp._result=req._x/req._y;
        }
        break;
    case '%':
        if(req._y == 0){
            resp._result=2;
        }
        else {
            resp._result=req._x%req._y;
        }
        break;
    default:
        resp._code=3;
        break;
    }
    return resp;
}
static Response calculatorHelper(const Request& req)
{
    Response resp(0,0,req._x,req._y,req._op);
    switch (req._op) 
    {
    case '+':
        resp._result=req._x+req._y;
        break;
    case '-':
        resp._result=req._x-req._y;
        break;
    case '/':
        if(req._y==0){
            resp._code=1;
        }
        else{
            resp._result=req._x/req._y;
        }
        break;
    case '%':
        if(req._y==0)
            resp._code=2;
        else{
            resp._result=req._x%req._y;
        }
        break;
    default:
        resp._code=3;
        break;
    }
    return resp;
}
void caculator2(int sock)
{
    std::string inbuffer;
    while(true)
    {
        //这里接受到的信息就会存放到inbuffer里面
        bool res=Recv(sock, &inbuffer);
        if(!res){
            break;
        }
        std::string package=DeCode(inbuffer);
        if(package.empty()){
            continue;
        }
        Request req;
        req.Deserialize(package);
        Response resp;
        resp=calculatorHelper(req);
        std::string respString = resp.Serialize();
        //解析方法本身就属于类外方法
        respString = EnCode(respString);
        Send(sock, respString);
    }
}
void caculator(int sock)
{
    std::string inbuffer;
    while(true)
    {
        bool res=Recv(sock, &inbuffer);
        if(!res){
            break;
        }
        std::string package=DeCode(inbuffer);
        if(package.empty()){
            continue;
        }
        Request req;
        req.Deserialize(inbuffer);
        Response resp;
        resp=calculatorHelper(req);
        std::string respString = resp.Serialize();
        respString = EnCode(respString);
        Send(sock, respString);
    }
}
// void calculator(int sock)
// {
//     std::string inbuffer;
//     while(true)
//     {
//         bool res=Recv(sock, &inbuffer);
//         if(!res){
//             break;
//         }
//         std::string package=DeCode(inbuffer);
//         if(package.empty()){
//             continue;
//         }
//         //将数据提取出来以后经过反序列化以后
//         Request req;
//         req.Deserialize(package);
//         //只是将结构化的数据提取出来还不够还必须经过计算得出结果
//         Response resp=calculatorHelper(req);
//         std::string respString = resp.Serialize();
//         respString=EnCode(respString);
//         Send(sock, respString);
//     }
// }
// void calculator(int sock)
// {
//     std::string inbuffer;
//     while(true)
//     {
//         bool res=Recv(sock, &inbuffer);
//         if(!res){
//             break;
//         }
//         std::string package=DeCode(inbuffer);
//         if(package.empty()){
//             continue;
//         }
//         LogMessage(NORMAL, "%s", package.c_str());
//         Request req;
//         req.Deserialize(package);
//     }
// }

int main(int argc,const char* argv[])
{
    if(argc != 2){
        exit(-1);
    }
    std::unique_ptr<TcpServer> server(new TcpServer(atoi(argv[1])));
    server->Start();
    return 0;
}